package org.example.terminal;

import org.example.dao.DepartmentDAO;
import org.example.dao.EmployeeDAO;
import org.example.service.DepartmentService;
import org.example.service.EmployeeService;
import org.example.service.SalaryService;
import org.example.utils.DataUtils;
import org.example.utils.TableUtils;

import java.util.ArrayList;
import java.util.Scanner;

import static org.example.terminal.DepartmentMenu.query;

public class EmployeeMenu {
    public static void employeeMenu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("==================================");
        System.out.println("==========员工薪资管理==============");
        System.out.println("===1、员工新增======================");
        System.out.println("===2、员工修改======================");
        System.out.println("===3、员工删除======================");
        System.out.println("===4、员工查询======================");
        System.out.println("===5、退出模块======================");
        System.out.println("==================================");
        System.out.println("请输入要执行的功能序号：");

        int operation = 0;
        try {
            operation = scanner.nextInt();
            switch (operation) {
                case 1:
                    add(scanner);
                    employeeMenu();
                    break;
                case 2:
                    update(scanner);
                    employeeMenu();
                    break;
                case 3:
                    delete(scanner);
                    employeeMenu();
                    break;
                case 4:
                    EmployeeMenu.query();
                    employeeMenu();
                    break;
                case 5:
                    System.out.println("已退出部门管理！");
                    break;
                default:
                    System.out.println("您输入的功能序号不存在，请检查功能菜单并重新输入！");
                    employeeMenu();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            employeeMenu();
        }
    }

    public static boolean query() throws Exception {
        ArrayList<String[]> data = EmployeeService.getEmployeeInfo();
        if (data != null && data.isEmpty()) {
            throw new Exception("======当前员工数据为空======");
        }

        if (data != null) {
            String[] header = {"序号", "员工性别", "员工工号", "部门名称", "员工名称"};
            System.out.println("========员工数据查询========");
            TableUtils.printTable(header, DataUtils.listToStringArray(data));
        } else {
            System.out.println("错误！请联系技术人员");
        }
        return true;
    }

    private static void delete(Scanner scanner) throws Exception {
        System.out.println("========员工数据删除=========");
        boolean query = EmployeeMenu.query();
        if (!query) {
            return;
        }


        System.out.println("======请输入要删除的序号======");
        int num = 0;
        try {
            num = scanner.nextInt();
        } catch (Exception e) {
            throw new Exception("输入格式错误！请重新输入");
        }
        ArrayList<String[]> data = EmployeeService.getEmployeeInfo();
        //序号范围校验
        if (data != null && (num > data.size() || num < 1)) {
            throw new Exception("请正确输入员工对应的序号");
        }
        String id = "";
        if (data != null) {
            id = data.get(num - 1)[0];
        }


        //service删除
        if (EmployeeService.deleteEmployee(id)) {
            if (SalaryService.deleteSalaryById(id)){
                System.out.println("========员工数据删除成功！=========");
            }else {
                System.out.println("========员工数据删除成功！该员工并没有对应薪资记录=========");
            }

        } else {
            System.out.println("========员工数据删除失败！=========");
        }

    }

    private static void update(Scanner scanner) throws Exception {
        EmployeeMenu.query();


        System.out.println("======请输入修改的数据序号======");

        int num = 0;
        try {
            num = scanner.nextInt();
        } catch (Exception e) {
            System.out.println("输入不合法");
        }
        ArrayList<String[]> data = EmployeeService.queryEmployee();
        //序号范围校验
        if (data != null && (num > data.size() || num < 1)) {
            throw new Exception("请正确输入员工对应的序号");
        }

        //已选择部门数据回显
        String departName = "";
        String empId = "";
        String empName = "";
        String empSex = "";
        if (data != null) {
            departName = data.get(num - 1)[3];
            empId = data.get(num - 1)[0];
            empSex = data.get(num - 1)[1];
            empName = data.get(num - 1)[4];
        }
        System.out.println("=======已选择[" + departName + "]部门员工：[" + empName + "]========");
        System.out.println("======员工名字原数据为：" + empName + "，请输入员工的新名字======");
        String empNewName = scanner.next();
        if (empName != null && !empName.matches("[\u4e00-\u9fa5]+")) {
            throw new Exception("输入的员工名称有错误，请输入符合要求的员工名称(员工名称为中文，不包含空格、字母、数字)");
        }


        System.out.println("=====员工性别原数据为：" + empSex + ",请选择员工的性别（1:男，2:女）======");
        int genderNum = 0;
        String empNewSex = "";
        try {
            genderNum = scanner.nextInt();
        } catch (Exception e) {
            throw new Exception("输入格式错误！请重新输入");
        }
        //如果gender不等于1或者不等于2
        if (genderNum != 1 && genderNum != 2) {
            System.out.println("输入的性别编号错误，请重新输入");
            return;
        } else {
            if (genderNum == 1) {
                empNewSex = "MALE";
            } else {
                empNewSex = "FEMALE";
            }
        }


        //service修改
        if (EmployeeService.updateEmployee(empNewName, empNewSex, empId)) {
            System.out.println("========数据修改成功=======");
            System.out.println("=========================");
        } else {
            System.out.println("========数据修改失败=======");
            System.out.println("=========================");
        }

    }

    private static void add(Scanner scanner) throws Exception {
        System.out.println("==================================");
        System.out.println("==========员工数据新增==============");

        //检测部门数据是否存在
        ArrayList<String[]> data = DepartmentService.getDepartmentInfo();
        if (data != null && data.isEmpty()) {
            throw new Exception("===当前部门数据为空，请先添加部门数据===");
        }

        DepartmentMenu.query();
        System.out.println("======请选择一个部门进行员工数据添加======");


        int num = 0;
        try {
            num = scanner.nextInt();
        } catch (Exception e) {
            throw new Exception("输入格式错误！请重新输入");
        }

        //序号范围校验
        if (data != null && (num > data.size() || num < 1)) {
            throw new Exception("请正确输入部门对应的序号");
        }
        //已选择部门数据回显
        String departName = "";
        String departId = "";
        if (data != null) {
            departName = data.get(num - 1)[1];
            departId = data.get(num - 1)[0];
        }
        System.out.println("=======已选择的部门：" + departName + "========");

        System.out.println("======请输入新员工姓名======");
        String empName = "";
        try {
            empName = scanner.next();
        } catch (Exception e) {
            throw new Exception("输入格式错误！请重新输入");
        }
        if (empName != null && !empName.matches("[\u4e00-\u9fa5]+")) {
            throw new Exception("输入的员工名称有错误，请输入符合要求的员工名称(员工名称为中文，不包含空格、字母、数字)");
        }

        System.out.println("======请输入新员工性别（1：男；2：女）======");
        int gender = 0;
        String empSex = "";
        try {
            gender = scanner.nextInt();
        } catch (Exception e) {
            throw new Exception("输入格式错误！请重新输入");
        }
        //如果gender不等于1或者不等于2
        if (gender != 1 && gender != 2) {
            throw new Exception("输入的性别编号有错误，请重新输入");
        } else {
            if (gender == 1) {
                empSex = "MALE";
            } else {
                empSex = "FEMALE";
            }
        }


        //service新增
        if (EmployeeService.addEmployee(departId, empName, empSex, departName)) {
            System.out.println("========数据添加成功=======");
            System.out.println("=========================");
        } else {
            System.out.println("========数据添加失败=======");
            System.out.println("=========================");
        }
    }


}
